/*
	VerbApplyEnhancement.cpp

	This verb calls the use() mutator on an enhancement object
	(c)2008 Palestar, Jeff Everett
*/


#include "Debug/Assert.h"
#include "Debug/Profile.h"
#include "World/NounTarget.h"
#include "VerbApplyEnhancement.h"
#include "Game/CargoEnhancement.h"
#include "Network/LogClient.h"		// LogLevels
#include "GameContext.h"

//-------------------------------------------------------------------------------

IMPLEMENT_FACTORY( VerbApplyEnhancement, Verb );

BEGIN_PROPERTY_LIST( VerbApplyEnhancement, Verb )
	ADD_TRANSMIT_PROPERTY( m_pEnhancement );
END_PROPERTY_LIST();

VerbApplyEnhancement::VerbApplyEnhancement()
{}

VerbApplyEnhancement::VerbApplyEnhancement( Noun * a_pUser, Noun *a_pEnhancement)
	: m_pEnhancement(a_pEnhancement)
{
	if ( a_pEnhancement != NULL )
		attachVerb( a_pUser );
}

//----------------------------------------------------------------------------

Verb::Priority VerbApplyEnhancement::priority() const
{
	return CRITICAL;
}

Verb::Scope VerbApplyEnhancement::scope() const
{
	return SERVER;
}

bool VerbApplyEnhancement::client() const
{
	return true;
}

bool VerbApplyEnhancement::queue() const
{
	return false;
}


//----------------------------------------------------------------------------

bool VerbApplyEnhancement::canAttach( Noun * pNoun )
{
	if (!validate(m_pEnhancement) && validate(pNoun))
		return false;
	return true;
}

void VerbApplyEnhancement::onExecute()
{
	// if server apply as child
	NounShip * pShip = WidgetCast<NounShip>( target() );
	CargoEnhancement *pCargo = WidgetCast<CargoEnhancement>(m_pEnhancement);
	if(pCargo != NULL)
	{
		Factory * pFactory = Factory::findFactory( pCargo->enhancementKey());
		if ( pFactory != NULL )
		{
			NounEnhancement *pEnh = NULL;
			// create the enhancement that we will add to the ship
				// attach the enhancement
			pEnh = WidgetCast<NounEnhancement>( pFactory->createWidget() );
				// apply or use?
			if(pEnh->applyType() == NounEnhancement::ApplyType::AT_ATTACH)
			{
				if(context()->server())
				{
					pShip->attachNode(pEnh);
					// detach the cargo
					pShip->detachNode(pCargo);
					pCargo->setDetach();
					// set message to player
					pShip->privateChat( CharString().format( "<color;ffffff>Enhancement: Attaching <color;%6.6x>%s</color> onto %s.", 
						pEnh->color().RGB(),pEnh->GetName(), m_pTarget->name() ) );
					pShip->context()->user()->onLog( CharString().format("Enhancement:Apply:%s:%s",
						pEnh->GetName(),m_pTarget->name()),LOG_NORMAL );

					// save off storage with this "active" ship
					context()->user()->saveStorage(pShip);
				}
				pShip->InitializeTrail();
			}
			else if(pEnh->applyType() == NounEnhancement::ApplyType::AT_USE)
			{
				// use enhancement
				pEnh->onUse(pShip);

				if(context()->server())
				{
					// detach the cargo
					pShip->detachNode(pCargo);
					pCargo->setDetach();

					pShip->privateChat( CharString().format( "<color;ffffff>Enhancement: Using <color;%6.6x>%s</color> on %s.", 
						pEnh->color().RGB(),pEnh->GetName(), m_pTarget->name() ) );
					pShip->context()->user()->onLog( CharString().format("Enhancement:Using:%s:%s",
						pEnh->GetName(),m_pTarget->name()),LOG_NORMAL );

					// save off storage with this "active" ship
					context()->user()->saveStorage(pShip);
				}
			}
		}
	}
}

bool VerbApplyEnhancement::canApply( CargoEnhancement * a_pCargo, NounShip * a_pUser )
{
	if ( a_pCargo == NULL || a_pUser == NULL )
		return false;		// invaid pointers
	if(!a_pUser->canApplyEnhancement(a_pCargo))
		return false;

	return true;

}

//----------------------------------------------------------------------------
// EOF
